package com.example.docking.service.impl;

import com.example.docking.dao.jindie.GenericDataItem;
import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.SupplierData;
import com.example.docking.service.SupplierService;
import com.example.docking.util.Constants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SupplierServiceImpl implements SupplierService {
    public static SupplierData SUPPLIERDATA;
    @Override
    public SupplierData getSupplier(DataServiceImpl service, String supplierName) throws Exception {
        if (SUPPLIERDATA != null) {
            return SUPPLIERDATA;
        }
        SupplierData supplierData = new SupplierData();
        Gson gson = new Gson();
        // 设置分页参数
        int page = 1; // 初始页码
        int pageSize = 10; // 每页记录数
        HashMap<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        map.put("page_size", String.valueOf(pageSize));
        String depart_id = "";

        ApiResult DataResult = service.SelectDataDetail(Constants.SUPPLIER, map, "");
        Type SupplierDataType = new TypeToken<ResponseData<GenericDataItem<SupplierData>>>() {
        }.getType();
        ResponseData<GenericDataItem<SupplierData>> responseSupplierData = gson.fromJson(DataResult.getBody(), SupplierDataType);
        GenericDataItem<SupplierData> genericDataItemDepart = responseSupplierData.getData();
        // 解析分页信息
        String count = genericDataItemDepart.getCount();
        int countNum = Integer.valueOf(count);
        int Pagesum = pageSize;
        List<SupplierData> SupplierData = genericDataItemDepart.getRows();

        List<SupplierData> allSupplierData = new ArrayList<>(SupplierData);
        // 如果不是最后一页，继续获取下一页数据
        while (Pagesum < countNum) {
            page++;
            map.put("page", String.valueOf(page));
            DataResult = service.SelectDataDetail(Constants.DEPARTMENT, map, "");
            responseSupplierData = gson.fromJson(DataResult.getBody(), SupplierDataType);
            genericDataItemDepart = responseSupplierData.getData();
            SupplierData = genericDataItemDepart.getRows();
            allSupplierData.addAll(SupplierData);
            Pagesum = pageSize * page;
        }
//        supplierData = allSupplierData.get(0);
        boolean departmentFound = false;
        for (SupplierData departmentData : allSupplierData) {
            if (departmentData.getName().equals(supplierName)) {
                supplierData = departmentData;
                departmentFound = true;
                break;
            }
        }
        if (!departmentFound) {
            // 这里可以根据业务需求添加未找到部门时的处理逻辑，比如抛异常等
            // 目前简单返回空字符串
            supplierData = allSupplierData.get(0);
        }

        SUPPLIERDATA = supplierData;
        return supplierData;
    }
}
